package nl.hr.minor.mobilelife.j1213.jlr.flow;

import nl.hr.minor.mobilelife.j1213.jlr.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class FlowLevelActivity extends Activity implements OnClickListener, IPanelToActivityCallbacks {

	private FlowPanel panel;
	private TextView levelCodeLabel, rowcolCountLabel, flowsLabel, movesLabel, pipeLabel;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.flow_level);
		panel = (FlowPanel) findViewById(R.id.activity_flow_level_levelpanel);

		levelCodeLabel = (TextView) findViewById(R.id.activity_flow_level_label_levelcode);
		rowcolCountLabel = (TextView) findViewById(R.id.activity_flow_level_label_rowcolcount);
		flowsLabel = (TextView) findViewById(R.id.activity_flow_level_label_label_flows);
		movesLabel = (TextView) findViewById(R.id.activity_flow_level_label_label_moves);
		pipeLabel = (TextView) findViewById(R.id.activity_flow_level_label_label_pipe);

		Intent intent = getIntent();
		int levelCode = intent.getIntExtra("levelCode", -1);
		if (levelCode == -1){} else {	
			loadLevel(levelCode);	
		}
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.activity_flow_level_label_button_previous_level:
			onPreviousLevel();
			break;
		case R.id.activity_flow_level_label_button_reset_level:
			onResetLevel();
			break;
		case R.id.activity_flow_level_label_button_next_level:
			onNextLevel();
			break;
		default:
			// No default action needed
		}
	}
	
	public void loadLevel(int levelCode) {
		panel.loadLevel(levelCode);	
	}
	
	public void onPreviousLevel() {
		try {
			if(panel.getLevel() != null) {
				int curLevelCode = panel.getLevel().levelCode;
				if(LevelManager.getInstance(this).getLowestLevelCode() == curLevelCode) {
					// Meh, that will not work
				} else {
					loadLevel(curLevelCode - 1);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void onResetLevel() {
		panel.reset();
	}
	
	public void onNextLevel() {
		try {
			if(panel.getLevel() != null) {
				int curLevelCode = panel.getLevel().levelCode;
				if(LevelManager.getInstance(this).getHighestLevelCode() == curLevelCode) {
					// hm, that will not work
				} else {
					loadLevel(curLevelCode + 1);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void onLevelLoadingDone(int rowcolcount, int levelCode) {
		this.levelCodeLabel.setText("Level " + levelCode);
		this.rowcolCountLabel.setText(rowcolcount + "x" + rowcolcount);
	}

	@Override
	public void onLevelComplete() {
		onNextLevel();
	}

	@Override
	public void onStatisticsUpdate(int currentFlowsDone, int totalFlows, int currentMovesDone, float percentagePipeDone) {
		this.flowsLabel.setText("flows: " + currentFlowsDone + "/" + totalFlows);
		this.movesLabel.setText("moves: " + Integer.toString(currentMovesDone));
		this.pipeLabel.setText("pipe: " + percentagePipeDone + "%");
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}
